<?php

    defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR);

    include "cnx.php";
    ini_set('memory_limit', '500M');
    ini_set ('max_execution_time', 0);

    // Charger le fichier.
    require_once "ClassAllocine.php";
    require_once "ClassMovie.php";
    require_once "ClassMovieDetail.php";

    define('ALLOCINE_PARTNER_KEY', '100043982026');
    define('ALLOCINE_SECRET_KEY', '29d185d98c984a359e6e6f26a0474269');

    $allocine = new Allocine(ALLOCINE_PARTNER_KEY, ALLOCINE_SECRET_KEY);
 
    $onlyNewMovies = false;
    $searchForAll  = false;

    $listMovie = Movie::getListMovieFromFolders($onlyNewMovies);

    foreach ($listMovie as $movie) {
        try
        {            
            $movie->getMovieDetails();

            if( $searchForAll || empty($movie->listMovieDetails)){
                // Envoi de la requête avec les paramètres, et enregistrement des résultats dans $donnees.
                
                $donnees = objectToArray(json_decode($allocine->search($movie->name)));
                $donnees = $donnees['feed'];
                
                // Pas de résultat ?
                if ( !isset($donnees['movie'] ) )
                {
                    // Afficher un message d'erreur.
                    echo '<p>Pas de résultat pour "' . $movie->name . '"</p>';
                }
                
                else
                {
                    // Pour chaque résultat de film.
                    foreach ( $donnees['movie'] as $film )
                    {
                        $filmDetail = objectToArray(json_decode($allocine->get(27405)));
                        $filmDetail = $filmDetail['movie'];
                        $movieDetail = new MovieDetail();

                        $movieDetail->movieId      = $movie->id;
                        $movieDetail->code           = (isset($film["code"])) ? $film["code"] : "";
                        $movieDetail->title          = (isset($filmDetail["title"])) ? $filmDetail["title"] : "";
                        $movieDetail->synopsis       = (isset($filmDetail["synopsis"])) ? $filmDetail["synopsis"] : "";
                        $movieDetail->posterURL      = (isset($filmDetail["poster"]["href"])) ? $filmDetail["poster"]["href"] : "";
                        $movieDetail->productionYear = (isset($film["productionYear"])) ? $film["productionYear"] : "";
                        $movieDetail->releaseDate    = (isset($film["release"]["releaseDate"])) ? $film["release"]["releaseDate"] : "";
                        $movieDetail->directors      = (isset($film["castingShort"]["directors"])) ? $film["castingShort"]["directors"] : "";
                        $movieDetail->actors         = (isset($film["castingShort"]["actors"])) ? $film["castingShort"]["actors"] : "";
                        $movieDetail->pressRating    = (isset($film["statistics"]["pressRating"])) ? $film["statistics"]["pressRating"] : "";
                        $movieDetail->userRating     = (isset($film["statistics"]["userRating"])) ? $film["statistics"]["userRating"] : "";
                        $movieDetail->state          = 1;
                        
                        $movieDetail->downloadPoster();
                        die;
                    }
                }
            }
            
        }
        
        // En cas d'erreur.
        catch ( ErrorException $e )
        {
            // Afficher un message d'erreur.
            echo "Erreur " . $e->getCode() . ": " . $e->getMessage();
        }
        
    }

    function objectToArray($d) {
        if (is_object($d)) {
            // Gets the properties of the given object
            // with get_object_vars function
            $d = get_object_vars($d);
        }
 
        if (is_array($d)) {
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return array_map(__FUNCTION__, $d);
        }
        else {
            // Return array
            return $d;
        }
    }

?>